package com.abu.crms.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.abu.crms.config.common.Resp;
import com.abu.crms.config.mvc.LoginInterceptor;
import com.abu.crms.pojo.Manager;
import com.abu.crms.pojo.Message;
import com.abu.crms.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/crms/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("all/{userId}")
    public Resp queryAllMessageByIds(@PathVariable("userId") Long userId) {
        String managerId = LoginInterceptor.getManagerId();
        List<Message> messageList = this.messageService.list(new QueryWrapper<Message>()
                .eq("manager_id", managerId)
                .eq("user_id", userId)
                .select("gmt_create", "content"));
        return Resp.ok().data("messageList", messageList);
    }

    @PostMapping("entity")
    public Resp addMessage(
            @RequestParam("applyId") Long applyId,
            @RequestParam("content") String content,
            @RequestParam("userId") Long userId
    ) {
        String managerId = LoginInterceptor.getManagerId();
        Message message = new Message();
        message
                .setManagerId(Convert.toLong(managerId))
                .setApplyReportId(applyId)
                .setContent(content)
                .setUserId(userId);
        boolean flag = this.messageService.save(message);
        if (flag) {
            return Resp.ok().msg("留言成功");
        }
        return Resp.error().msg("留言失败");
    }

}

